package com.boolib.shopconfig.durid;



import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import java.sql.SQLException;

@Component
@Configuration
@PropertySource("classpath:datasource.properties")
@ServletComponentScan(basePackages = {"com.boolib"})
public class DruidDataSourceConfig {


    @Value("${db.driverClassName}")
    private String driver;

    @Value("${db.url}")
    private String url;

    @Value("${db.username}")
    private String username;

    @Value("${db.password}")
    private String password;

    @Value("${db.validationQuery}")
    private String query;

    @Value("${db.minIdle}")
    private int minIdle;

    @Value("${db.druid.username}")
    private String druidUsername;


    @Value("${db.druid.password}")
    private String druidPassword;



    @Bean
    public DruidDataSource druidDataSource(){

        DruidDataSource druidDataSource = new DruidDataSource();

        druidDataSource.setDriverClassName(driver);

        druidDataSource.setUrl(url);

        druidDataSource.setUsername(username);

        druidDataSource.setPassword(password);

        druidDataSource.setMinIdle(minIdle==0?5:minIdle);

        druidDataSource.setValidationQuery(query.equals("")?"select 1 ":query);

        druidDataSource.removeAbandoned();

        druidDataSource.setRemoveAbandonedTimeout(80);

        druidDataSource.setTestOnBorrow(true);

        druidDataSource.setTestOnReturn(true);
        try {
            druidDataSource.setFilters("stat,wall");
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return druidDataSource;
    }


    /**
     * 注册Servlet信息， 配置监控视图
     *
     * @return
     */
    @Bean
    @ConditionalOnMissingBean
    public ServletRegistrationBean druidServlet() {
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
//
//        //白名单：
//        servletRegistrationBean.addInitParameter("allow","192.168.6.195");
//        //IP黑名单 (存在共同时，deny优先于allow) : 如果满足deny的话提示:Sorry, you are not permitted to view this page.
//        servletRegistrationBean.addInitParameter("deny","192.168.6.73");
        //登录查看信息的账号密码, 用于登录Druid监控后台
        servletRegistrationBean.addInitParameter("loginUsername", druidUsername.equals("")?"admin":druidUsername);
        servletRegistrationBean.addInitParameter("loginPassword", druidPassword.equals("")?"123456":druidPassword);
        //是否能够重置数据.
        servletRegistrationBean.addInitParameter("resetEnable", "true");
        return servletRegistrationBean;

    }

    /**
     * 注册Filter信息, 监控拦截器
     *
     * @return
     */
    @Bean
    @ConditionalOnMissingBean
    public FilterRegistrationBean filterRegistrationBean() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new WebStatFilter());
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        return filterRegistrationBean;
    }


}
